package com.galeno.movie;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.galeno.json.MovieBean;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;
import java.util.*;

/**
 * @author galeno
 * @Title:
 * @Description:
 * @date 2021/7/2710:39
 */
public class TopN {
    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
        Configuration conf=new Configuration();
        conf.set("fs.defaultFS","file:/");
        Job job = Job.getInstance(conf,"TopN");

        job.setMapperClass(TopNMapper.class);
        job.setReducerClass(TopNRed.class);

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        FileInputFormat.setInputPaths(job,new Path("D:\\mrdata\\movie\\input"));
        FileOutputFormat.setOutputPath(job,new Path("D:\\mrdata\\movie\\out"));

        job.waitForCompletion(true);




    }
    static class TopNMapper extends Mapper<LongWritable, Text,IntWritable, IntWritable>{
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String line = value.toString();
            MovieBean movieBean = JSON.parseObject(line, MovieBean.class);
            int movie = movieBean.getMovie();


            context.write(new IntWritable(movie),new IntWritable(1));

        }
    }
    static class  TopNRed extends Reducer<Text,IntWritable,Text,IntWritable>{
        HashMap<String,Integer> map=new HashMap<>();
        @Override
        protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
            int count=0;
            for (IntWritable value : values) {
                count++;
            }
            map.put(key.toString(),count);


        }

        @Override
        protected void cleanup(Context context) throws IOException, InterruptedException {
            Set<Map.Entry<String, Integer>> set = map.entrySet();
            ArrayList<Map.Entry<String, Integer>> ls = new ArrayList<>(set);
            Collections.sort(ls, new Comparator<Map.Entry<String, Integer>>() {
                @Override
                public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                    return o2.getValue()-o1.getValue();
                }
            });
            Text key=new Text();
            IntWritable v=new IntWritable();
            for (int i = 0; i <Math.min(3,ls.size()); i++) {
                Map.Entry<String,Integer> entry=ls.get(i);
            }
            context.write(key,v);


        }
    }


}
